home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 6522 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.3 KB

  1. Path: vixen.cso.uiuc.edu!usenet
  2. From: homer@uiuc.edu
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: resetting a character array
  5. Date: 9 Feb 1996 03:51:09 GMT
  6. Organization: University of Illinois at Urbana
  7. Message-ID: <4fegbd$1o3@vixen.cso.uiuc.edu>
  8. References: <4fbt20$4o3@cloner3.netcom.com>
  9. Reply-To: n-dade@uiuc.edu
  10. NNTP-Posting-Host: homer.apr.uiuc.edu
  11. X-Newsreader: IBM NewsReader/2 v1.2
  12.  
  13. In <4fbt20$4o3@cloner3.netcom.com>, Manuel Hernandez <ManuelHe@ix.netcom.com> writes:
  14. >This must be a total beginners question. How do you reset
  15. >a character array so you can enter a string into a variable 
  16. >over and over again? This is what I have so far.
  17.  
  18. istream::getline() overwrites the previous contents of the array, so there is no
  19. need to "reset" the array to empty. (If you want the array to represent the
  20. empty string then you put the 0 character in the first element of the array:
  21.  name[0] = 0;
  22. but you don't have to for getline().)
  23.  
  24. >void main()
  25. >{  
  26. >   char name[20];
  27. >   char answer;
  28. >            
  29. >   do
  30. >     {  
  31. >    cout << "Enter a name" << endl;
  32. >    cin.getline(name,20);
  33. >                
  34. >    cout << name << " is a nice name. Enter another? " << endl;
  35. >    cin >> answer;
  36. >                    
  37. >     }
  38. >    while (answer == 'y');
  39. >    return;
  40. >}
  41.  
  42. I copy/pasted/compiled and ran this little program and I think I see the
  43. problem you are dealing with: It runs like this:
  44.  
  45. Enter a name
  46.                     user enters "Nicolas" and presses return.
  47. Nicolas
  48. Nicolas is a nice name. Enter another?
  49.                     user enters "y" and presses return
  50. y
  51. Enter a name
  52.  is a nice name. Enter another?
  53.                     user enters "n" and presses return
  54. n
  55.  
  56. What has happened is that cin still contains the "end of line" character that
  57. the user entered after the "y". This is because when you read from cin you
  58. only read one single "y". Then the next time you read from cin you use
  59. getline() which reads until it finds an "end of line" character. It finds that
  60. immediately, and you don't get the name you wanted.
  61.  
  62. The solution is to read the entire response to the "Enter another?" question,
  63. including the end-of-line, before reading the next name from cin. I'd
  64. suggest using getline() to read the "y" as well. Here are the 3 lines that
  65. change:
  66.  
  67.    char answer[10]; // <--- changed answer to be an array as well
  68. ..
  69.    cin.getline(answer, 10); // <--- read the entire response
  70. ..
  71.     while (answer[0] == 'y'); // <--- check the 1st letter of the response is "y"
  72.  
  73.  
  74.  
  75.